/**
 * 
 */
package vn.support.connection.response.processor.impl;

import org.apache.log4j.Logger;

import vn.support.connection.Connection;
import vn.support.connection.response.processor.AbstractResponseProcessor;
import vn.support.core.dto.DataObject;
import vn.support.core.dto.DataObjectUtil;
import vn.support.core.dto.SessionData;

/**
 * @author nttuyen
 *
 */
public class LoginProcessor extends AbstractResponseProcessor {

	private static final Logger log = Logger.getLogger(LoginProcessor.class);
	
	public LoginProcessor() {
		super();
	}
	
	public DataObject process(DataObject data) {
		String xml = data.get(Connection.DTO_RESPONSE_DATA);
		if(!xml.startsWith("<?xml")) {
			log.debug("Response is not XML: " + xml);
			return data;
		}
		
		SessionData sessionData = this.entityFactory.create(xml, "swiftwinapp", SessionData.class).iterator().next();
		
		//Save session data to system object
		DataObject object = DataObjectUtil.get(DataObject.SYSTEM_DATA_OBJECT_NAME);
		object.set(SessionData.class.getName(), sessionData);
		DataObjectUtil.set(DataObject.SYSTEM_DATA_OBJECT_NAME, object);
		return data;
	}
	
}
